import { useCallback, useEffect, useState } from "react";
import request from "@/utils/request";
import { stringify } from "qs";
import { message } from "antd";

async function quickQueryLabelGetByCode(params) {
	return request(`/new-common/quickQueryLabel/getByCode?${stringify(params)}`, {
		loading: false
	});
}
async function quickQueryLabelSaveOrUpdate(params) {
	return request(`/new-common/quickQueryLabel/saveOrUpdate`, {
		method: "post",
		data: params,
		loading: true
	});
}

const getCode = tableKey => {
	return "ColsSettingStore" + tableKey;
};
/**
 * 获取【当前表格】缓存的配置;
 * @returns
 */
const geStoreColList = async tableKey => {
	// return storeList;
	const code = getCode(tableKey);
	return new Promise((resolve, reject) => {
		quickQueryLabelGetByCode({ code })
			.then(res => {
				const { data, success } = res;

				if (success && data && data.param && typeof data.param === "string") {
					resolve(JSON.parse(data.param));
				} else {
					resolve([]);
				}
			})
			.catch(res => {
				resolve([]);
			});
	});
};
/**
 * 保存配置
 * @param {*}  当前需要保存的搜索条件 {name,code,checked}
 */
const saveApi = (tableKey, list) => {
	const API_DATA = list.map(e => ({ name: e.name, code: e.code, checked: e.checked, lock: e.lock }));
	const code = getCode(tableKey);
	// setStoreList(API_DATA);

	quickQueryLabelSaveOrUpdate({ code, param: JSON.stringify(API_DATA) })
		.then(res => {
			const { data, success, msg } = res;
			if (success) {
				message.success(msg || "保存成功");
			} else {
				message.warning(msg || "保存失败");
			}
		})
		.catch(res => {});
};
/**
 *
 * @param {*} d sortColumnsForTable
 * @returns
 */
function getFilterCols(d) {
	if (!Array.isArray(d)) return [];
	return d
		.filter(e => e.hidden !== true)
		.map(e => {
			return {
				...e,
				checked: e.checked !== false
			};
		});
}
export const useColsSetting = ({ tableKey, columns }) => {
	// 远程数据
	const [remoteData, setRemoteData] = useState([]);

	const initColsSetting = useCallback(() => {
		geStoreColList(tableKey).then(d => {
			setRemoteData(d);
		});
	}, [tableKey]);
	const updateRemoteData = useCallback(
		d => {
			setRemoteData(d);
		},
		[setRemoteData]
	);
	const saveRemoteData = useCallback(
		d => {
			updateRemoteData(d);
			saveApi(tableKey, d);
		},
		[updateRemoteData, tableKey]
	);
	// 实际表格配置数据
	const [realColumns, setRealColumns] = useState([]);
	const [filterColumns, setFilterColumns] = useState([]);
	useEffect(() => {
		const filterCols = getFilterCols(columns);
		setFilterColumns(filterCols);

		const flag1 = Array.isArray(remoteData);
		const flag2 = flag1 && filterCols.length == remoteData.length; // 未 增删配置
		const flag3 = flag2 && filterCols.every(e => remoteData.find(ee => ee.code === e.code)); // 未修改 配置 code
		if (flag3) {
			const sortFilterCols = remoteData.map(e => {
				const f = filterCols.find(ee => ee.code == e.code);
				return {
					...f,
					checked: e.checked,
					lock: e.lock
				};
			});
			setRealColumns(sortFilterCols);
		} else {
			setRealColumns(filterCols);
		}
	}, [columns, remoteData]);

	return {
		filterColumns,
		realColumns,
		saveRemoteData,
		initColsSetting
	};
};
